package com.crowntit.domain.goods.service;

import com.crowntit.domain.goods.adapter.port.IGroupBuyProductPort;
import com.crowntit.domain.goods.adapter.port.IOriProductPort;
import com.crowntit.domain.goods.adapter.repository.IGoodsRepository;
import com.crowntit.domain.goods.model.entity.GoodsConfigInfoEntity;
import com.crowntit.domain.goods.model.entity.QueryGoodsConfigEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class GoodsService implements IGoodsService{

    @Resource
    private IOriProductPort productPort;
    @Resource
    private IGroupBuyProductPort groupBuyProductPort;
    @Resource
    private IGoodsRepository repository;

    /**
     * 模拟订单发货完成，修改订单状态为deal_done已完成
     * @param orderId
     */
    @Override
    public void changeOrderDealDone(String orderId) throws Exception {
        repository.changeOrderDealDone(orderId);
    }

    @Override
    public GoodsConfigInfoEntity queryGoodsConfigInfo(QueryGoodsConfigEntity queryGoodsEntity) throws IOException {
        // 查找是否商品配置了团购活动
        GoodsConfigInfoEntity goodsConfigInfoEntity = groupBuyProductPort.queryGoodsConfig(queryGoodsEntity);

        // 如果没配置团购活动
        if (goodsConfigInfoEntity == null){
            goodsConfigInfoEntity = GoodsConfigInfoEntity.builder()
                    .groupBuyTeamList(null)
                    .goods(productPort.queryGoodsInfo(queryGoodsEntity.getProductId()))
                    .isGroupBuyConfig(false)
                    .build();
        }

        return goodsConfigInfoEntity;
    }
}
